<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getMyTeachersAPI, addTeacherAPI } from '@/api/student'

const loading = ref(false)
const dialogVisible = ref(false)
const myTeachers = ref([])
const formRef = ref(null)

const joinForm = ref({
  teacherId: '',
  relationship: ''
})

const rules = {
  teacherId: [
    { required: true, message: '请输入教师ID', trigger: 'blur' },
    { type: 'number', message: '教师ID必须为数字', trigger: 'blur' }
  ],
  relationship: [
    { required: true, message: '请选择与教师的关系', trigger: 'change' }
  ]
}

// 获取我的教师列表
const getMyTeachers = async () => {
  loading.value = true
  try {
    const { data } = await getMyTeachersAPI()
    if (data.code === 200) {
      myTeachers.value = data.data
    } else {
      ElMessage.error(data.msg || '获取教师列表失败')
    }
  } catch (error) {
    console.error('获取教师列表失败:', error)
    ElMessage.error('获取教师列表失败')
  } finally {
    loading.value = false
  }
}

// 显示加入班级弹窗
const showJoinDialog = () => {
  dialogVisible.value = true
  joinForm.value = {
    teacherId: '',
    relationship: ''
  }
}

// 通过教师ID加入班级
const handleJoinByTeacherId = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const { data } = await addTeacherAPI(joinForm.value.teacherId, {
          relationship: joinForm.value.relationship
        })
        if (data.code === 200) {
          ElMessage.success('成功加入班级！')
          dialogVisible.value = false
          getMyTeachers() // 刷新列表
        } else {
          ElMessage.error(data.msg || '加入班级失败')
        }
      } catch (error) {
        console.error('加入班级失败:', error)
        ElMessage.error('加入班级失败')
      }
    }
  })
}

onMounted(() => {
  getMyTeachers()
})
</script>

<template>
  <div class="join-class-container">
    <div class="header">
      <h2>我的班级</h2>
      <el-button type="primary" @click="showJoinDialog">加入新班级</el-button>
    </div>

    <!-- 已加入的班级列表 -->
    <el-table
      v-loading="loading"
      :data="myTeachers"
      style="width: 100%"
    >
      <!-- <el-table-column prop="teacherId" label="教师ID" width="100" /> -->
      <el-table-column prop="name" label="教师姓名" />
      <el-table-column prop="relationshipType" label="关系">
        <template #default="scope">
          <span v-if="scope.row.relationshipType === 'student'">学生</span>
          <span v-else-if="scope.row.relationshipType === 'assistant'">助教</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column prop="department" label="所属院系" width="180" />
    </el-table>

    <!-- 加入班级弹窗 -->
    <el-dialog
      v-model="dialogVisible"
      title="加入班级"
      width="400px"
    >
      <el-form
        ref="formRef"
        :model="joinForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="教师ID" prop="teacherId">
          <el-input v-model.number="joinForm.teacherId" placeholder="请输入教师ID" />
        </el-form-item>
        <el-form-item label="关系" prop="relationship">
          <el-select v-model="joinForm.relationship" placeholder="请选择与教师的关系">
            <el-option label="学生" value="student" />
            <el-option label="助教" value="assistant" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleJoinByTeacherId">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>


<style scoped>
.join-class-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

h2 {
  margin: 0;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 